﻿layui.config({
    version: true,
    base: '/Plugin/'
}).extend({ //设定模块别名
    md5: 'md5', //如果 mymod.js 是在根目录，也可以不用设定别名
    common: 'common' //相对于上述 base 目录的子目录
    }).use(['element', 'jquery', 'layer', 'table', 'form', 'common'], function () {
    var element = layui.element,
        layer = layui.layer,
        table = layui.table,
        form = layui.form,
        common = layui.common,
        $ = layui.jquery;

    //必须在框架内使用
    common.noneIframe();

    var tableMenuGroup = null;

    var tableMenu = table.render({
        elem: '#tableMenu',
        url: '/Ashx/admin.ashx', //数据接口
        method: 'post',
        toolbar: "#toolbarBtn",
        where: { functionname: 'jhy_menu_query' },
        cols: [[ //表头           
            { field: 'mg_name', title: '菜单组别', event: "mg_id", style: "cursor:pointer" },
            { field: 'menu_name', title: '菜单名称', event: "menu_name", style: "cursor:pointer" },
            { field: 'menu_url', title: 'URL', event: "menu_url", style: "cursor:pointer" },
            { title: '操作', toolbar: "#toolbarMenuDone", width: 112 }
        ]]
    });



    //头部工具条操作
    table.on('toolbar(tableMenu)', function (obj) {
        var checkStatus = table.checkStatus(obj.config.id);
        switch (obj.event) {
            case 'manageMenuGroup':
                //####################
                parent.layer.open({
                    title: "菜单组管理",
                    type: 1,
                    area: ["100%", "100%"],
                    btn: "关闭窗口",
                    content: $("#layerMenuGroup").html(),
                    success: function (layero, index) {
                        parent.layui.$("#formMenuGroup").find("input:first").val("");
                        var toolbarDone = $("#toolbarDone").html();
                        tableMenuGroup = parent.layui.table.render({
                            elem: '#tableMenuGroup',
                            url: '/Ashx/admin.ashx', //数据接口
                            method: 'post',
                            where: { functionname: 'jhy_menugroup_query' },
                            cols: [[ //表头           
                                { field: 'mg_name', title: '菜单组名称', edit: true },
                                { title: '操作', toolbar: toolbarDone, width: 180, align: "center" }
                            ]]
                        });
                    }
                });



                //###################
                break;
            case 'addMenu':
                parent.layui.layer.open({
                    title: "添加菜单",
                    type: 1,
                    btn: "保存",
                    skin: "jhy-layer-css jhy-layer-css-select",
                    content: $("#layerMenu").html(),
                    success: function (layero, index) {
                        //清空input
                        parent.layui.$("#formMenu").find("#menuName").val("");
                        parent.layui.$("#formMenu").find("#menuUrl").val("")
                        //加载菜单组
                        $.ajax({
                            type: "post",
                            url: "/Ashx/admin.ashx",
                            dataType: 'json',
                            data: { functionname: "jhy_menugroup_query" },
                            success: function (msg) {
                                var selecthtml = "";
                                $.each(msg["data"], function (a, b) {
                                    selecthtml += "<option value=" + b.mg_id + ">" + b.mg_name + "</option>";
                                })
                                parent.layui.$("#selectMenuGroup").html(selecthtml);
                                parent.layui.form.render("select", "formMenu");
                            }
                        });
                    }, yes: function (index, layero) {
                        var menu_name = $.trim(parent.layui.$("#formMenu").find("#menuName").val());
                        var menu_url = $.trim(parent.layui.$("#formMenu").find("#menuUrl").val());
                        var mg_id = $.trim(parent.layui.$("#formMenu").find("#selectMenuGroup").val());
                        if (menu_name == "") {
                            parent.layui.layer.msg("菜单名称不能为空");
                            return false
                        }
                        if (menu_url == "") {
                            parent.layui.layer.msg("菜单网址不能为空");
                            return false
                        }
                        $.ajax({
                            type: "post",
                            url: "/Ashx/admin.ashx",
                            data: { functionname: "jhy_menu_add", menu_name: menu_name, menu_url: menu_url, mg_id: mg_id },
                            success: function (msg) {
                                if (msg == "true") {
                                    parent.layui.layer.msg("成功");
                                    tableMenu.reload();
                                    parent.layui.layer.close(index);
                                } else {
                                    parent.layui.layer.msg("失败");
                                    tableMenu.reload();
                                    parent.layui.layer.close(index);
                                }
                            }
                        });


                    }

                });
                break;
        };
    });

    //start 菜单组########################################################

    //添加菜单组
    parent.layui.form.on('submit(btnSave)', function (data) {
        var mg_name = $.trim(data.field.menuGroupName);
        if (mg_name == '') {
            parent.layui.layer.msg("请输入内容");
        } else {
            $.ajax({
                type: "post",
                url: "/Ashx/admin.ashx",
                data: { functionname: "jhy_menugroup_add", mg_name: mg_name },
                success: function (msg) {
                    if (msg == 'true') {
                        parent.layui.layer.msg("成功");
                        tableMenuGroup.reload();
                    } else {
                        parent.layui.layer.msg("失败");
                        tableMenuGroup.reload();
                    }
                }
            });
        }
        return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
    });

    //删除，上移，下移操作
    parent.layui.table.on('tool(tableMenuGroup)', function (obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
        var data = obj.data; //获得当前行数据
        var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
        var tr = obj.tr; //获得当前行 tr 的DOM对象
        var mg_id = data.mg_id;
        if (layEvent === 'up') { //上移
            var mg_field = 'up';
            var mg_para = data.mg_order;
            $.ajax({
                type: "post",
                url: "/Ashx/admin.ashx",
                data: { functionname: "jhy_menugroup_edit", mg_id: mg_id, mg_field: mg_field, mg_para: mg_para },
                success: function (msg) {
                    if (msg == 'true') {
                        parent.layui.layer.msg("成功");
                        tableMenuGroup.reload();
                    } else {
                        parent.layui.layer.msg("失败");
                        tableMenuGroup.reload();
                    }
                }
            });
        } else if (layEvent === 'down') { //下移
            var mg_field = 'down';
            var mg_para = data.mg_order;
            $.ajax({
                type: "post",
                url: "/Ashx/admin.ashx",
                data: { functionname: "jhy_menugroup_edit", mg_id: mg_id, mg_field: mg_field, mg_para: mg_para },
                success: function (msg) {
                    if (msg == 'true') {
                        parent.layui.layer.msg("成功");
                        tableMenuGroup.reload();
                    } else {
                        parent.layui.layer.msg("失败");
                        tableMenuGroup.reload();
                    }
                }
            });
        } else if (layEvent === 'delete') { //删除                        
            parent.layui.layer.confirm('真的删除[' + data.mg_name + ']组?', { icon: 3, title: '删除菜单组确认' }, function (index) {
                $.ajax({
                    type: "post",
                    url: "/Ashx/admin.ashx",
                    data: { functionname: "jhy_menugroup_delete", mg_id: mg_id },
                    success: function (msg) {
                        if (msg == 'true') {
                            parent.layui.layer.msg("成功");
                            obj.del();
                        } else {
                            parent.layui.layer.msg("失败");
                            tableMenuGroup.reload();
                        }
                    }
                });
                parent.layui.layer.close(index);
                //向服务端发送删除指令
            });
        }
    });
    //修改菜单组名称
    parent.layui.table.on('edit(tableMenuGroup)', function (obj) {
        var mg_para = obj.value,
            data = obj.data,
            mg_id = data.mg_id,
            mg_field = obj.field;
        $.ajax({
            type: "post",
            url: "/Ashx/admin.ashx",
            data: { functionname: "jhy_menugroup_edit", mg_id: mg_id, mg_field: mg_field, mg_para: mg_para },
            success: function (msg) {
                if (msg == 'true') {
                    parent.layui.layer.msg("成功");
                } else {
                    parent.layui.layer.msg("失败");
                    tableMenuGroup.reload();
                }
            }
        });

    });
    // end 修改菜单组########################################################

    // start修改菜单########################################################
    table.on("tool(tableMenu)", function (obj) {
        var data = obj.data; //获得当前行数据
        var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
        var tr = obj.tr; //获得当前行 tr 的DOM对象

        if (layEvent === 'mg_id') { //查看
            layer.open({
                title: "修改菜单组（修改完成后将刷新菜单）",
                type: 1,
                btn: "保存",
                skin:"jhy-layer-css jhy-layer-css-select",
                content: "<div class='layui-form' style='padding:10px;'><div class='layui-form-item'><div class='layui-input-inline'><select id='menugroup'></select></div></div></div>",
                success: function (layero, index) {
                    //加载菜单组
                    $.ajax({
                        type: "post",
                        url: "/Ashx/admin.ashx",
                        dataType: 'json',
                        data: { functionname: "jhy_menugroup_query" },
                        success: function (msg) {
                            var selecthtml = "";
                            $.each(msg["data"], function (a, b) {
                                selecthtml += "<option value=" + b.mg_id + ">" + b.mg_name + "</option>";
                            })
                            layero.find("#menugroup").html(selecthtml);
                            form.render("select");
                        }
                    });
                }, yes: function (index, layero) {
                    var menu_id = data.menu_id;
                    var menu_field = "mg_id";
                    var menu_para = $("#menugroup").val();
        
                    $.ajax({
                        type: "post",
                        url: "/Ashx/admin.ashx",                   
                        data: { functionname: "jhy_menu_edit", menu_id: menu_id, menu_field:menu_field,menu_para:menu_para },
                        success: function (msg) {
                            if (msg == "true") {
                                layer.msg("成功");
                                tableMenu.reload();
                                layer.close(index);
                            } else
                            {
                                layer.msg("失败");
                                tableMenu.reload();
                                layer.close(index);
                            }
                        }
                    });
                }
            });

        } else if (layEvent === 'menu_name') { //删除
            layer.prompt({
                formType: 0,
                value: data.menu_name,
                title: '修改菜单名称'               
            }, function (value, index, elem) {
                var menu_id = data.menu_id;
                var menu_field = "menu_name";
                var menu_para = value;
                $.ajax({
                    type: "post",
                    url: "/Ashx/admin.ashx",
                    data: { functionname: "jhy_menu_edit", menu_id: menu_id, menu_field: menu_field, menu_para: menu_para },
                    success: function (msg) {
                        if (msg == "true") {
                            layer.msg("成功");
                            obj.update({
                                menu_name: value
                            });
                        } else {
                            layer.msg("失败");                           
                        }
                    }
                });
                layer.close(index);
            });

        } else if (layEvent === 'menu_url') { //编辑 
            layer.prompt({
                formType: 0,
                value: data.menu_url,
                title: '修改菜单网址'
            }, function (value, index, elem) {
                var menu_id = data.menu_id;
                var menu_field = "menu_url";
                var menu_para = value;
                $.ajax({
                    type: "post",
                    url: "/Ashx/admin.ashx",
                    data: { functionname: "jhy_menu_edit", menu_id: menu_id, menu_field: menu_field, menu_para: menu_para },
                    success: function (msg) {
                        if (msg == "true") {
                            layer.msg("成功");
                            obj.update({
                                menu_url: value
                            });
                        } else {
                            layer.msg("失败");
                        }
                    }
                });
                layer.close(index);
            });
        } else if (layEvent === 'delete') {
            var menu_id = data.menu_id;
            layer.confirm('真的删除[' + data.menu_name + ']吗？', {icon:3,title:"菜单删除确认"}, function (index) {
                $.ajax({
                    type: "post",
                    url: "/Ashx/admin.ashx",
                    data: { functionname: "jhy_menu_delete", menu_id: menu_id},
                    success: function (msg) {
                        if (msg == "true") {
                            layer.msg("成功");
                            obj.del();
                        } else {
                            layer.msg("失败");
                            tableMenu.reload();
                        }
                    }
                });                
                layer.close(index);                
            });
        } else if (layEvent === 'up') {
            var menu_id = data.menu_id;
            var menu_field = "up";
            var menu_para = data.menu_order;
            $.ajax({
                type: "post",
                url: "/Ashx/admin.ashx",
                data: { functionname: "jhy_menu_edit", menu_id: menu_id, menu_field: menu_field, menu_para: menu_para },
                success: function (msg) {
                    if (msg == "true") {
                        layer.msg("成功");
                        tableMenu.reload();
                    } else {
                        layer.msg("失败");
                        tableMenu.reload();
                    }
                }
            });
        } else if (layEvent === 'down') {
            var menu_id = data.menu_id;
            var menu_field = "down";
            var menu_para = data.menu_order;
            $.ajax({
                type: "post",
                url: "/Ashx/admin.ashx",
                data: { functionname: "jhy_menu_edit", menu_id: menu_id, menu_field: menu_field, menu_para: menu_para },
                success: function (msg) {
                    if (msg == "true") {
                        layer.msg("成功");
                        tableMenu.reload();
                    } else {
                        layer.msg("失败");
                        tableMenu.reload();
                    }
                }
            });
        }
    });

    // end 修改菜单########################################################

});  
